<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ev.global.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ev.watchers.html">Watchers</a></div>
 <div class="up"><a href="book.ev.html">Ev</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="ev.examples" class="chapter">
 <h1>范例</h1>

 <div class="example" id="example-3517">
  <p><strong>Example #1 Simple timers</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;and&nbsp;start&nbsp;timer&nbsp;firing&nbsp;after&nbsp;2&nbsp;seconds<br /></span><span style="color: #0000BB">$w1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;function&nbsp;()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"2&nbsp;seconds&nbsp;elapsed\n"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;and&nbsp;launch&nbsp;timer&nbsp;firing&nbsp;after&nbsp;2&nbsp;seconds&nbsp;repeating&nbsp;each&nbsp;second<br />//&nbsp;until&nbsp;we&nbsp;manually&nbsp;stop&nbsp;it<br /></span><span style="color: #0000BB">$w2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"is&nbsp;called&nbsp;every&nbsp;second,&nbsp;is&nbsp;launched&nbsp;after&nbsp;2&nbsp;seconds\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"iteration&nbsp;=&nbsp;"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Stop&nbsp;the&nbsp;watcher&nbsp;after&nbsp;5&nbsp;iterations<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">()&nbsp;==&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">and&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Stop&nbsp;the&nbsp;watcher&nbsp;if&nbsp;further&nbsp;calls&nbsp;cause&nbsp;more&nbsp;than&nbsp;10&nbsp;iterations<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">()&nbsp;&gt;=&nbsp;</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #007700">and&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;stopped&nbsp;timer.&nbsp;It&nbsp;will&nbsp;be&nbsp;inactive&nbsp;until&nbsp;we&nbsp;start&nbsp;it&nbsp;ourselves<br /></span><span style="color: #0000BB">$w_stopped&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">::</span><span style="color: #0000BB">createStopped</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;function(</span><span style="color: #0000BB">$w</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Callback&nbsp;of&nbsp;a&nbsp;timer&nbsp;created&nbsp;as&nbsp;stopped\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Stop&nbsp;the&nbsp;watcher&nbsp;after&nbsp;2&nbsp;iterations<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">iteration</span><span style="color: #007700">()&nbsp;&gt;=&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">and&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #FF8000">//&nbsp;Loop&nbsp;until&nbsp;Ev::stop()&nbsp;is&nbsp;called&nbsp;or&nbsp;all&nbsp;of&nbsp;watchers&nbsp;stop<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;Start&nbsp;and&nbsp;look&nbsp;if&nbsp;it&nbsp;works<br /></span><span style="color: #0000BB">$w_stopped</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">start</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #DD0000">"Run&nbsp;single&nbsp;iteration\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">RUN_ONCE</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Restart&nbsp;the&nbsp;second&nbsp;watcher&nbsp;and&nbsp;try&nbsp;to&nbsp;handle&nbsp;the&nbsp;same&nbsp;events,&nbsp;but&nbsp;don't&nbsp;block\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$w2</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">again</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">RUN_NOWAIT</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;function()&nbsp;{});<br />echo&nbsp;</span><span style="color: #DD0000">"Running&nbsp;a&nbsp;blocking&nbsp;loop\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #DD0000">"END\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <div class="example-contents"><p>以上例程的输出类似于：</p></div>
  <div class="example-contents screen">
<div class="cdata"><pre>
2 seconds elapsed
is called every second, is launched after 2 seconds
iteration = 1
is called every second, is launched after 2 seconds
iteration = 2
is called every second, is launched after 2 seconds
iteration = 3
is called every second, is launched after 2 seconds
iteration = 4
is called every second, is launched after 2 seconds
iteration = 5
Run single iteration
Callback of a timer created as stopped
Restart the second watcher and try to handle the same events, but don&#039;t block
Running a blocking loop
is called every second, is launched after 2 seconds
iteration = 8
is called every second, is launched after 2 seconds
iteration = 9
is called every second, is launched after 2 seconds
iteration = 10
END
</pre></div>
  </div>
 </div>
 <div class="example" id="example-3518">
  <p><strong>Example #2 Periodic timer. Tick each 10.5 seconds</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvPeriodic</span><span style="color: #007700">(</span><span style="color: #0000BB">0.</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$revents</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3519">
  <p><strong>Example #3 Periodic timer. Use reschedule callback</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Tick&nbsp;each&nbsp;10.5&nbsp;seconds<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">reschedule_cb&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$watcher</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$now</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$now&nbsp;</span><span style="color: #007700">+&nbsp;(</span><span style="color: #0000BB">10.5</span><span style="color: #007700">.&nbsp;-&nbsp;</span><span style="color: #0000BB">fmod</span><span style="color: #007700">(</span><span style="color: #0000BB">$now</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10.5</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvPeriodic</span><span style="color: #007700">(</span><span style="color: #0000BB">0.</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0.</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"reschedule_cb"</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$revents</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3520">
  <p><strong>Example #4 Periodic timer. Tick every 10.5 seconds starting at now</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Tick&nbsp;every&nbsp;10.5&nbsp;seconds&nbsp;starting&nbsp;at&nbsp;now<br /></span><span style="color: #0000BB">$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvPeriodic</span><span style="color: #007700">(</span><span style="color: #0000BB">fmod</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">now</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">10.5</span><span style="color: #007700">),&nbsp;</span><span style="color: #0000BB">10.5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$revents</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">time</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3521">
  <p><strong>Example #5 Wait until STDIN is readable</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Wait&nbsp;until&nbsp;STDIN&nbsp;is&nbsp;readable<br /></span><span style="color: #0000BB">$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvIo</span><span style="color: #007700">(</span><span style="color: #0000BB">STDIN</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">READ</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$watcher</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$revents</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"STDIN&nbsp;is&nbsp;readable\n"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">RUN_ONCE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3522">
  <p><strong>Example #6 Use some async I/O to access a socket</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Use&nbsp;some&nbsp;async&nbsp;I/O&nbsp;to&nbsp;access&nbsp;a&nbsp;socket&nbsp;*/<br /><br />//&nbsp;`sockets'&nbsp;extension&nbsp;still&nbsp;logs&nbsp;warnings<br />//&nbsp;for&nbsp;EINPROGRESS,&nbsp;EAGAIN/EWOULDBLOCK&nbsp;etc.<br /></span><span style="color: #0000BB">error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ERROR</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$e_nonblocking&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(</span><span style="color: #FF8000">/*EAGAIN&nbsp;or&nbsp;EWOULDBLOCK*/</span><span style="color: #0000BB">11</span><span style="color: #007700">,&nbsp;</span><span style="color: #FF8000">/*EINPROGRESS*/</span><span style="color: #0000BB">115</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;the&nbsp;port&nbsp;for&nbsp;the&nbsp;WWW&nbsp;service<br /></span><span style="color: #0000BB">$service_port&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getservbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tcp'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Get&nbsp;the&nbsp;IP&nbsp;address&nbsp;for&nbsp;the&nbsp;target&nbsp;host<br /></span><span style="color: #0000BB">$address&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gethostbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'google.co.uk'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;TCP/IP&nbsp;socket<br /></span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_create()&nbsp;failed:&nbsp;reason:&nbsp;"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Set&nbsp;O_NONBLOCK&nbsp;flag<br /></span><span style="color: #0000BB">socket_set_nonblock</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Abort&nbsp;on&nbsp;timeout<br /></span><span style="color: #0000BB">$timeout_watcher&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">(</span><span style="color: #0000BB">10.0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0.</span><span style="color: #007700">,&nbsp;function&nbsp;()&nbsp;use&nbsp;(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">stop</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">BREAK_ALL</span><span style="color: #007700">);<br />});<br /><br /></span><span style="color: #FF8000">//&nbsp;Make&nbsp;HEAD&nbsp;request&nbsp;when&nbsp;the&nbsp;socket&nbsp;is&nbsp;writable<br /></span><span style="color: #0000BB">$write_watcher&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvIo</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">WRITE</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$timeout_watcher</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$e_nonblocking</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Stop&nbsp;timeout&nbsp;watcher<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$timeout_watcher</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Stop&nbsp;write&nbsp;watcher<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"HEAD&nbsp;/&nbsp;HTTP/1.1\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Host:&nbsp;google.co.uk\r\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Connection:&nbsp;Close\r\n\r\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">socket_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$in</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$in</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Failed&nbsp;writing&nbsp;</span><span style="color: #0000BB">$in</span><span style="color: #DD0000">&nbsp;to&nbsp;socket"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_ERROR</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$read_watcher&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvIo</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">READ</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$re</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$e_nonblocking</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Socket&nbsp;is&nbsp;readable.&nbsp;recv()&nbsp;20&nbsp;bytes&nbsp;using&nbsp;non-blocking&nbsp;mode<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_recv</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MSG_DONTWAIT</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$out</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;All&nbsp;read<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Caught&nbsp;EINPROGRESS,&nbsp;EAGAIN,&nbsp;or&nbsp;EWOULDBLOCK<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">$e_nonblocking</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;});<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$address</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$service_port</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

  <div class="example-contents"><p>以上例程的输出类似于：</p></div>
  <div class="example-contents screen">
<div class="cdata"><pre>
HTTP/1.1 301 Moved Permanently
Location: http://www.google.co.uk/
Content-Type: text/html; charset=UTF-8
Date: Sun, 23 Dec 2012 16:08:27 GMT
Expires: Tue, 22 Jan 2013 16:08:27 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Connection: close


</pre></div>
  </div>
 </div>
 <div class="example" id="example-3523">
  <p><strong>Example #7 Embedding one loop into another</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br />*&nbsp;Try&nbsp;to&nbsp;get&nbsp;an&nbsp;embeddable&nbsp;event&nbsp;loop&nbsp;and&nbsp;embed&nbsp;it&nbsp;into&nbsp;the&nbsp;default&nbsp;event&nbsp;loop.<br />*&nbsp;If&nbsp;it&nbsp;is&nbsp;impossible,&nbsp;use&nbsp;the&nbsp;default<br />*&nbsp;loop.&nbsp;The&nbsp;default&nbsp;loop&nbsp;is&nbsp;stored&nbsp;in&nbsp;$loop_hi,&nbsp;while&nbsp;the&nbsp;embeddable&nbsp;loop&nbsp;is<br />*&nbsp;stored&nbsp;in&nbsp;$loop_lo(which&nbsp;is&nbsp;$loop_hi&nbsp;in&nbsp;the&nbsp;case&nbsp;no&nbsp;embeddable&nbsp;loop&nbsp;can&nbsp;be<br />*&nbsp;used).<br />*<br />*&nbsp;Sample&nbsp;translated&nbsp;to&nbsp;PHP<br />*&nbsp;http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9<br />*/<br /></span><span style="color: #0000BB">$loop_hi&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">EvLoop</span><span style="color: #007700">::</span><span style="color: #0000BB">defaultLoop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$loop_lo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$embed&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*<br />*&nbsp;See&nbsp;if&nbsp;there&nbsp;is&nbsp;a&nbsp;chance&nbsp;of&nbsp;getting&nbsp;one&nbsp;that&nbsp;works<br />*&nbsp;(flags'&nbsp;value&nbsp;of&nbsp;0&nbsp;means&nbsp;autodetection)<br />*/<br /></span><span style="color: #0000BB">$loop_lo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">embeddableBackends</span><span style="color: #007700">()&nbsp;&amp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">recommendedBackends</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;new&nbsp;</span><span style="color: #0000BB">EvLoop</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">embeddableBackends</span><span style="color: #007700">()&nbsp;&amp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">recommendedBackends</span><span style="color: #007700">())<br />&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">$loop_lo</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$embed&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvEmbed</span><span style="color: #007700">(</span><span style="color: #0000BB">$loop_lo</span><span style="color: #007700">,&nbsp;function&nbsp;()&nbsp;{});<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$loop_lo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$loop_hi</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3524">
  <p><strong>Example #8 Embedding loop created with kqueue backend into the default loop</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br />*&nbsp;Check&nbsp;if&nbsp;kqueue&nbsp;is&nbsp;available&nbsp;but&nbsp;not&nbsp;recommended&nbsp;and&nbsp;create&nbsp;a&nbsp;kqueue&nbsp;backend<br />*&nbsp;for&nbsp;use&nbsp;with&nbsp;sockets&nbsp;(which&nbsp;usually&nbsp;work&nbsp;with&nbsp;any&nbsp;kqueue&nbsp;implementation).<br />*&nbsp;Store&nbsp;the&nbsp;kqueue/socket-only&nbsp;event&nbsp;loop&nbsp;in&nbsp;loop_socket.&nbsp;(One&nbsp;might&nbsp;optionally<br />*&nbsp;use&nbsp;EVFLAG_NOENV,&nbsp;too)<br />*<br />*&nbsp;Example&nbsp;borrowed&nbsp;from<br />*&nbsp;http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9<br />*/<br /></span><span style="color: #0000BB">$loop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">EvLoop</span><span style="color: #007700">::</span><span style="color: #0000BB">defaultLoop</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$socket_loop&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$embed&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">supportedBackends</span><span style="color: #007700">()&nbsp;&amp;&nbsp;~</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">recommendedBackends</span><span style="color: #007700">()&nbsp;&amp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">BACKEND_KQUEUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;((</span><span style="color: #0000BB">$socket_loop&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvLoop</span><span style="color: #007700">(</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">BACKEND_KQUEUE</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$embed&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvEmbed</span><span style="color: #007700">(</span><span style="color: #0000BB">$loop</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$socket_loop</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$socket_loop&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$loop</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Now&nbsp;use&nbsp;$socket_loop&nbsp;for&nbsp;all&nbsp;sockets,&nbsp;and&nbsp;$loop&nbsp;for&nbsp;anything&nbsp;else<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3525">
  <p><strong>Example #9 Handle SIGTERM signal</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvSignal</span><span style="color: #007700">(</span><span style="color: #0000BB">SIGTERM</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$watcher</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"SIGTERM&nbsp;received\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$watcher</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3526">
  <p><strong>Example #10 Monitor changes of /var/log/messages</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Use&nbsp;10&nbsp;second&nbsp;update&nbsp;interval.<br /></span><span style="color: #0000BB">$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvStat</span><span style="color: #007700">(</span><span style="color: #DD0000">"/var/log/messages"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"/var/log/messages&nbsp;changed\n"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$attr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">attr</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$attr</span><span style="color: #007700">[</span><span style="color: #DD0000">'nlink'</span><span style="color: #007700">])&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Current&nbsp;size:&nbsp;%ld\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$attr</span><span style="color: #007700">[</span><span style="color: #DD0000">'size'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Current&nbsp;atime:&nbsp;%ld\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$attr</span><span style="color: #007700">[</span><span style="color: #DD0000">'atime'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Current&nbsp;mtime:&nbsp;%ld\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$attr</span><span style="color: #007700">[</span><span style="color: #DD0000">'mtime'</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">STDERR</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"`messages`&nbsp;file&nbsp;is&nbsp;not&nbsp;there!"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />});<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3527">
  <p><strong>Example #11 Monotor changes of /var/log/messages. Avoid missing updates by means of one second delay</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$timer&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">EvTimer</span><span style="color: #007700">::</span><span style="color: #0000BB">createStopped</span><span style="color: #007700">(</span><span style="color: #0000BB">0.</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1.02</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stat&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;1&nbsp;second&nbsp;after&nbsp;the&nbsp;most&nbsp;recent&nbsp;change&nbsp;of&nbsp;the&nbsp;file<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Current&nbsp;size:&nbsp;%ld\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$stat</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">attr</span><span style="color: #007700">()[</span><span style="color: #DD0000">'size'</span><span style="color: #007700">]);<br />});<br /><br /></span><span style="color: #0000BB">$stat&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvStat</span><span style="color: #007700">(</span><span style="color: #DD0000">"/var/log/messages"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0.</span><span style="color: #007700">,&nbsp;function&nbsp;()&nbsp;use&nbsp;(</span><span style="color: #0000BB">$timer</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Reset&nbsp;timer&nbsp;watcher<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$timer</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">again</span><span style="color: #007700">();<br />});<br /><br /></span><span style="color: #0000BB">$timer</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$stat</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
 <div class="example" id="example-3528">
  <p><strong>Example #12 Process status changes</strong></p>
  <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$pid&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pcntl_fork</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">$pid&nbsp;</span><span style="color: #007700">==&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">STDERR</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"pcntl_fork&nbsp;failed\n"</span><span style="color: #007700">);<br />}&nbsp;elseif&nbsp;(</span><span style="color: #0000BB">$pid</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$w&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EvChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$pid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">,&nbsp;function&nbsp;(</span><span style="color: #0000BB">$w</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$revents</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stop</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Process&nbsp;%d&nbsp;exited&nbsp;with&nbsp;status&nbsp;%d\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rpid</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$w</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rstatus</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;});<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">Ev</span><span style="color: #007700">::</span><span style="color: #0000BB">run</span><span style="color: #007700">();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Protect&nbsp;against&nbsp;Zombies<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">pcntl_wait</span><span style="color: #007700">(</span><span style="color: #0000BB">$status</span><span style="color: #007700">);<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//Forked&nbsp;child<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">exit(</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="ev.global.constants.html">预定义常量</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="ev.watchers.html">Watchers</a></div>
 <div class="up"><a href="book.ev.html">Ev</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
